define(function(require, exports, module) {
    'use strict';
    
    var rk = require('rk');

    $.widget('rk.groupSetCtrl', {
        options: {
            privateMsgId: 0,
            memberIdList: null,
            users: null,
            groupName: '',
            pmType: null
        },
        _create: function() {
            var me = this;
            var elem = me.element;
        },
        _init: function() {
            var me = this;
            var elem = me.element;
            var opt = me.options;

            var newMsgTpl = require('page/tmpl/privatemsg/groupSet.tpl');

            var itemData = [];
            for (var i = 0; i < opt.memberIdList.length; i++) {
                var item = {};
                item.icon = opt.users[opt.memberIdList[i]].icon;
                item.name = opt.users[opt.memberIdList[i]].name;
                item.id = opt.memberIdList[i];
                itemData.push(item);
            }

            var memberCount = opt.memberIdList.length;

            var newMsgHtml = rk.templateText(newMsgTpl, {
                memberCount: memberCount,
                list: itemData,
                groupName: opt.groupName,
                pmType: opt.pmType
            });

            elem.append(newMsgHtml);

            elem.find('.group-member-list').beScroll();

            elem.find('.pg-pm-close').click(function() {
                me.closeSet(opt.privateMsgId);
            });

            elem.find('.edit').click(function() {
                elem.find('.group-edit').show();
                elem.find('.clear').hide();
                elem.find('.group-edit > .edit-text').val(elem.find('.clear > span').text());
            });

            elem.find('.pg-btn-white').click(function() {
                elem.find('.group-edit').hide();
                elem.find('.clear').show();
            });

            elem.find('.pg-btn-blue').click(function() {
                var newName = elem.find('.group-edit > .edit-text').val();
                var oldName = elem.find('.clear > span').text();
                if (newName === oldName) {
                    rk.noticeError(rk.i18n('PRIVATEMSG_NO_CHANGE'));
                    return;
                }
                if (newName === '') {
                    rk.noticeError(rk.i18n('PRIVATEMSG_GROUP_NAME_NOT_NULL'));
                    return;
                }
                $.postJson('/json/sns_wx/update-title.action', {
                    title: newName,
                    privateMsgId: opt.privateMsgId
                }).done(function(json) {
                    if (json.status) {
                        if (json.status == 230001) {
                            rk.noticeError('您已被移出讨论组');
                        } else {
                            rk.noticeError(json.status);
                        }
                    } else {
                        elem.find('.group-edit').hide();
                        elem.find('.clear').show();
                        elem.find('.clear > span').text(elem.find('.group-edit > .edit-text').val());
                        $('#privatemsg').privateMsgCtrl("reorderList", opt.privateMsgId, 0, newName);
                        $('#privatemsg').privateMsgCtrl("resetGroupList", opt.privateMsgId, newName);
                    }
                }).error(function() {
                    elem.find('.group-edit').hide();
                    elem.find('.clear').show();
                    elem.find('.clear > span').text(elem.find('.group-edit > .edit-text').val());
                    $('#privatemsg').privateMsgCtrl("reorderList", opt.privateMsgId, 0, newName);
                    $('#privatemsg').privateMsgCtrl("resetGroupList", opt.privateMsgId, 0, newName);
                });
            });

            elem.find('.exit').click(function() {
                rk.confirm(rk.i18n('PRIVATEMSG_IS_EXIT_GROUP'), function() {
                    $.postJson('/json/sns_wx/quit-chat.action', {
                        privateMsgId: opt.privateMsgId
                    }).done(function(json) {
                        if (json.status) {
                            rk.noticeError(json.status);
                        } else {
                            elem.hide();
                            $('#privatemsg').privateMsgCtrl("removeList", opt.privateMsgId);
                        }
                    });
                });
            });

            elem.find('.add-btn').on("click", function() {
                var btn = $(this);
                var loadingBtn = btn.loading();
                var memberEl = $("#pm_new_autocomplete").find(':input');
                var memberIdList = (memberEl.data('wtype') == 'snsmultipeopleselector' ? memberEl.snsmultipeopleselector('val') : '') || [];
                if (memberIdList.length === 0) {
                    rk.noticeError(rk.i18n('PRIVATEMSG_MAST_ONE_OR_MORE_PARTICIPANTS'));
                    loadingBtn.loading("end");
                    return false;
                }
                var params = {
                    userIds: memberIdList.join(","),
                    privateMsgId: opt.privateMsgId
                };
                $.postJson("/json/sns_wx/join.action", params).done(function(json) {
                    if (json.status) {
                        if (json.status == 230001) {
                            rk.noticeError('您已被移出讨论组');
                            loadingBtn.loading("end");
                        } else {
                            rk.noticeError(json.status);
                        }
                        return;
                    }
                    if (json.status === 0) {
                        if (opt.pmType === 1) {
                            me.closeSet(opt.privateMsgId);
                            $('#privatemsg').privateMsgCtrl("reorderList", opt.privateMsgId);
                        } else {
                            me.closeSet(json.data);
                            $('#privatemsg').privateMsgCtrl("reorderList", json.data, 0, 0, 'tophover');
                        }

                    }
                }).fail(function(def, code, message) {});
            });
            //depart selector
            $("#pm_new_autocomplete").find(':input').snsmultipeopleselector({
                position: {
                    my: 'left top+2'
                },
                excludeUsers: opt.memberIdList
            });

            elem.show();
        },
        destroy: function() {
            var me = this;
            var elem = me.element;
            var opt = me.options;

            $("#pm_new_autocomplete").find(':input').snsmultipeopleselector('clear');
            elem.hide();
            elem.empty();
        },
        closeSet: function(pmId) {
            var me = this;
            var opt = me.options;
            $('.pg-pm-messagebox').privateMsgDetailCtrl('showDetail');
            me.destroy();
            $('.pg-pm-messagebox').privateMsgDetailCtrl({
                privateMsgId: pmId,
                pageNo: 1,
                pageSize: 20,
                isGroup: 1
            });
        }
    });
});
